<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace app\admin\controller;

use api\mall\model\CultureModel;
use cmf\controller\AdminBaseController;
use think\db\Query;

/**
 * Description of CultureController
 *
 * @author PC
 */
class CultureController extends AdminBaseController
{

    /**
     * 文旅管理
     * @adminMenu(
     *     'name'   => '文旅管理',
     *     'parent' => 'admin/Culture/Index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文旅管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();

        $cultureModel = new CultureModel();
        $culturesQuery = $cultureModel->alias('a');
        $cultures = $culturesQuery
            ->where(function (Query $query) use ($param) {
                $keyword = empty($param['keyword']) ? '' : $param['keyword'];
                if (!empty($keyword)) {
                    $query->where('name', 'like', "%$keyword%");
                }
            })
            ->order('update_time', 'DESC')
            ->paginate(20);

        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('pages', $cultures->items());
        $this->assign('page', $cultures->render());
        return $this->fetch('index');
    }

    /**
     * 添加文旅
     * @adminMenu(
     *     'name'   => '添加文旅',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文旅',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加文旅提交
     * @adminMenu(
     *     'name'   => '添加文旅提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文旅提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $data = $this->request->param();

        $data['gallery'] = [];
        if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            foreach ($data['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($data['gallery'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
        }

        // 判断gallery最多为10张图片
        if (count($data['gallery']) > 10) {
            $this->error("最多上传10张图片");
        }

        $data['create_time'] = time();
        $data['update_time'] = time();

        if (!empty($data['video'])) {
            $data['video'] = cmf_asset_relative_url($data['video']);
        }

        $cultureModel = new CultureModel();
        $cultureModel->adminAddCulture($data);
        $this->success("保存成功");
    }

    // public function add()
    // {
    //     $type = $this->request->route('type/d', 1);
    //     $this->assign('type', $type);
    //     $storeModel = new StoreModel();
    //     $items = $storeModel->types();
    //     $this->assign('item_types', $items);
    //     return $this->fetch();
    // }

    /**
     * 编辑文旅
     * @adminMenu(
     *     'name'   => '编辑文旅',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文旅',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $cultureModel = new CultureModel();
        $data = $cultureModel->where('id', $id)->find();
        $this->assign('data', $data);

        return $this->fetch();
    }

    /**
     * 编辑文旅提交
     * @adminMenu(
     *     'name'   => '编辑文旅提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文旅提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();

        $data['gallery'] = [];
        if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            foreach ($data['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($data['gallery'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
        }
        unset($data['photo_names']);
        unset($data['photo_urls']);
        
        // 判断gallery最多为10张图片
        if (count($data['gallery']) > 10) {
            $this->error("最多上传10张图片");
        }        
        
        $data['update_time'] = time();

        if (!empty($data['video'])) {
            $data['video'] = cmf_asset_relative_url($data['video']);
        }

        $cultureModel = new CultureModel();

        $culture = CultureModel::find($data['id']);
        $culture->save($data);

        // $cultureModel->where('id', $data['id'])->update($data);
        $this->success("保存成功");
    }

    /**
     * 文旅删除
     * @adminMenu(
     *     'name'   => '文旅删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文旅删除',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function delete()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
            $cultureModel = new CultureModel();
            $cultureModel->where('id', $param['id'])->delete();
            $this->success("删除成功！", '');

        } else {
            $this->error("删除失败！");
        }
    }
}
